/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.navigator;

import com.vaadin.data.Item;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;
import java.util.Collection;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition.ProcessState;
import org.ow2.bonita.light.LightProcessDefinition;
import org.ow2.bonita.light.LightTaskInstance;
import org.processbase.touchprocess.ui.template.NavigationList;
import org.processbase.touchprocess.ui.TouchProcessApplication;
import org.processbase.touchprocess.ui.generator.BarResource;
import org.processbase.touchprocess.ui.template.ProcessButton;
import org.processbase.touchprocess.ui.generator.GeneratedLayout;
import org.processbase.ui.core.bonita.forms.XMLProcessDefinition;
import org.processbase.ui.core.bonita.forms.XMLTaskDefinition;

/**
 *
 * @author mgubaidullin
 */
public class NewProcessesNavigationView extends NavigationList {

    @Override
    public void initUI() {
        super.initUI();
        table.addContainerProperty("type", Button.class, null);
        table.setColumnWidth("type", 25);
        table.setColumnAlignment("type", Table.ALIGN_RIGHT);
        table.addContainerProperty("process", ProcessButton.class, null);
        table.addContainerProperty("name", String.class, null);
        table.setVisibleColumns(new Object[]{"type", "process"});
//        table.setColumnExpandRatio("task", 1);
    }

    @Override
    public void refreshTable() {
        table.removeAllItems();
        try {
            Collection<LightProcessDefinition> processes = TouchProcessApplication.getCurrent().getBpmModule().getLightProcessDefinitions(ProcessState.ENABLED);
            for (LightProcessDefinition process : processes) {
                Item item = table.addItem(process);
                ProcessButton btn = new ProcessButton(process);
                btn.addListener(this);
                item.getItemProperty("process").setValue(btn);
                item.getItemProperty("name").setValue(process.getName() + " " + process.getVersion());
            }
            this.rowCount = processes.size();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        table.setSortContainerPropertyId("name");
        table.setSortAscending(true);
        table.sort();
        setCaption("Processes (" + rowCount + ")");
    }

    @Override
    public void layoutClick(LayoutClickEvent event) {
        super.layoutClick(event);
        try {
            ProcessButton btn = (ProcessButton) event.getSource();
            LightProcessDefinition process = btn.getProcess();
            LightProcessDefinition refreshProcess = TouchProcessApplication.getCurrent().getBpmModule().getLightProcessDefinition(process.getUUID());
            if (refreshProcess.getState() != ProcessState.ENABLED) {
                table.removeItem(process);
            } else {
                table.setValue(process);
                openStartView(process);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            showError(ex.toString());
        }
    }

    public void openStartView(LightProcessDefinition process) {
        try {
            BarResource barResource = new BarResource(process.getUUID());
            XMLProcessDefinition xmlProcess = barResource.getXmlProcessDefinition();
            if (!xmlProcess.isSkipEntryPageFlow()) {
                GeneratedLayout generatedLayout = new GeneratedLayout();
                generatedLayout.setProcessDefinition(process);
                generatedLayout.setBarResource(barResource);
                TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getDetailsView().setDetailsLayout(generatedLayout);
                generatedLayout.initUI();
            } else {
                TouchProcessApplication.getCurrent().getBpmModule().startNewProcess(process.getUUID());
                showImportantInformation(TouchProcessApplication.getCurrent().getPbMessages().getString("processStarted"));
                refreshTable();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            showError(ex.getMessage());
        }
    }

    public void openTaskLayout(LightTaskInstance task) {
        try {
            BarResource barResource = new BarResource(task.getProcessDefinitionUUID());
            XMLProcessDefinition xmlProcess = barResource.getXmlProcessDefinition();
            XMLTaskDefinition taskDef = xmlProcess.getTasks().get(task.getActivityName());
            if (taskDef != null && !taskDef.isSkipEntryPageFlow() /*check that forms is defined*/) {
                GeneratedLayout generatedLayout = new GeneratedLayout();
                generatedLayout.setTask(TouchProcessApplication.getCurrent().getBpmModule().getTaskInstance(task.getUUID()));
                generatedLayout.setBarResource(barResource);
                TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getDetailsView().setDetailsLayout(generatedLayout);
                generatedLayout.initUI();
            } else if (taskDef != null && taskDef.isSkipEntryPageFlow()) {
                TouchProcessApplication.getCurrent().getBpmModule().startTask(task.getUUID(), true);
                TouchProcessApplication.getCurrent().getBpmModule().finishTask(task.getUUID(), true);
                showImportantInformation(TouchProcessApplication.getCurrent().getPbMessages().getString("taskExecuted"));
                refreshTable();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            showError(ex.getMessage());
        }
    }
}
